//
//  AddressListViewController.m
//  Products_B
//
//  Created by lanou on 16/6/2.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "AddressListViewController.h"
#import "AddressListTableViewCell.h"
#import "ChuanzhiModel.h"
#import "SureDingdanViewController.h"
#import "AddressViewController.h"
@interface AddressListViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *tempModelArray;

@end

@implementation AddressListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tempModelArray = [NSMutableArray array];
    
    self.navigationItem.title = @"收货地址";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64 - 50) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AddressListTableViewCell" bundle:nil] forCellReuseIdentifier:@"addressListTableViewCell"];
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeight - 114, kWidth, 50)];
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addButton.frame = CGRectMake(50, 10, kWidth - 100, 30);
    [addButton setTitle:@"添加地址" forState:UIControlStateNormal];
    addButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [addButton addTarget:self action:@selector(addButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [addButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    addButton.layer.borderWidth = 1;
    addButton.layer.borderColor = [UIColor orangeColor].CGColor;
    [bottomView addSubview:addButton];
    [self.view addSubview:bottomView];
 
    NSLog(@"%@",self.view);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"*********%ld",_chuanzhiModelArray.count);
    return _chuanzhiModelArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BOOL isOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"isOn"];
    if (isOn) {
        if (indexPath.section == 0) {
            // alloc init的目的是为了走cell类中的initWith...方法
            AddressListTableViewCell *cell = [[AddressListTableViewCell alloc] init];
            // 如果switch的开关为开，则列表中第一个cell为数组中最后一个model
            ChuanzhiModel *model = self.chuanzhiModelArray.lastObject;
            cell.model = model;
            return cell;
        } else {
            // xib中，dequeueReusableCellWithIdentifier方法内部会调用cell类中的awakeFromNib方法
            AddressListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addressListTableViewCell" forIndexPath:indexPath];
            ChuanzhiModel *model = self.chuanzhiModelArray[indexPath.section - 1];
            cell.model = model;
            return cell;
        }
    } else {
        if (indexPath.section == 0) {
            AddressListTableViewCell *cell = [[AddressListTableViewCell alloc] init];
            // 如果switch的开关为关，遍历model数组，将标记为默认标识为YES的那个model对应第一个cell
            for (int i = 0; i < _chuanzhiModelArray.count; i++) {
                 ChuanzhiModel *model = self.chuanzhiModelArray[i];
                NSLog(@" ++++++++ %d",model.isMoren);
                if (model.isMoren) {
                    // 将标识为YES的model重新组合成一个新的model数组
                    [self.tempModelArray addObject:model];
                }
            }
            // 将最后一个标识为YES的model删除，然后插入到第一个位置
            [self.chuanzhiModelArray removeObject:self.tempModelArray.lastObject];
            [self.chuanzhiModelArray insertObject:self.tempModelArray.lastObject atIndex:0];
            // 此时_chuanzhiModelArray的顺序已经发生了变化
            cell.model = self.chuanzhiModelArray.firstObject;
            return cell;
        } else {
            AddressListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addressListTableViewCell" forIndexPath:indexPath];
            ChuanzhiModel *model = self.chuanzhiModelArray[indexPath.section];
            cell.model = model;
            return cell;
        }
    }
}

#pragma mark - 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 必须通过数组找到控制器，不可以alloc init
    SureDingdanViewController *sureVC = self.navigationController.viewControllers[2];
    AddressListTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    sureVC.namePhoneLabel.text = cell.model.nameString;
    sureVC.addressLabel2.text = cell.model.addressString;
    NSLog(@"++++++++%@",cell.model.nameString);
    [self.navigationController popToViewController:sureVC animated:YES];
}


// 返回区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

// 返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)addButtonAction:(UIButton *)button {
    AddressViewController *addressVC = [[AddressViewController alloc] init];
    // block传值
    // 点击保存按钮时回调,susreVC不要用allo init
    SureDingdanViewController *sureVC = self.navigationController.viewControllers[2];
//    __block AddressViewController *blockSelf = sureVC;
    addressVC.block = ^(ChuanzhiModel *model) {
        // 将值传给sureVC的属性
        sureVC.namePhoneLabel.text = [NSString stringWithFormat:@"%@    %@",model.nameString,model.phoneString];
        sureVC.addressLabel2.text = model.addressString;
    };
    [self.navigationController pushViewController:addressVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
